var crypto = require('crypto'),
	  fs = require('fs'),
    Item = require('../models/item.js');

module.exports = function(app) {
    app.get('/', function (req, res) {
      //判断是否是第一页，并把请求的页数转换成 number 类型
      var page = req.query.p ? parseInt(req.query.p) : 1;
      //查询并返回第 page 页的 10 个商品
      Item.getTen(null, page, function (err, items, total) {
        if (err) {
          items = [];
        } 
        res.render('index', {
          title: '拉勾购 —— 关注拉勾购，享受好折扣',
          items: items,
          page: page,
          isFirstPage: (page - 1) == 0,
          isLastPage: ((page - 1) * 10 + items.length) == total,
          user: req.session.user,
          success: req.flash('success').toString(),
          error: req.flash('error').toString()
        });
      });
    });

    app.get('/additem', function (req, res) {
        res.render('additem', {
            title: '拉勾购 —— 关注拉勾购，享受好折扣 添加商品',
            //user: req.session.user,
            success: req.flash('success').toString(),
            error: req.flash('error').toString()
        });
    });

    app.post('/additem', function (req, res) {
        var tag_baby = req.body.tag_baby,
            tag_toy = req.body.tag_toy,
            tag_cloth_man = req.body.tag_cloth_man,
            tag_cloth_lady = req.body.tag_cloth_lady,
            tag_cloth_shoe = req.body.tag_cloth_shoe;

        var tags = "";
        if(tag_baby) tags+='tag_baby;';
        if(tag_toy) tags+='tag_toy;';
        if(tag_cloth_man) tags+='tag_cloth_man;';
        if(tag_cloth_lady) tags+='tag_cloth_lady;';
        if(tag_cloth_shoe) tags+='tag_cloth_shoe;';

        var currentUser = req.session.user,
            item = new Item(req.body.name, req.body.info, req.body.price, tags, req.body.imgpath);
        item.save(function (err) {
            if (err) {
              req.flash('error', err); 
              return res.redirect('/');
            }
            req.flash('success', '发布成功!');
            res.redirect('/edititems');//新增成功跳转到主页
        });
      });

    app.get('/edititems', function (req, res) {
      var currentUser = req.session.user;
      //判断是否是第一页，并把请求的页数转换成 number 类型
      var page = req.query.p ? parseInt(req.query.p) : 1;
      //查询并返回第 page 页的 10 个商品
      Item.getTen(null, page, function (err, items, total) {
        if (err) {
          items = [];
        } 
        res.render('edititems', {
          title: '拉勾购 —— 关注拉勾购，享受好折扣 管理商品',
          items: items,
          page: page,
          isFirstPage: (page - 1) == 0,
          isLastPage: ((page - 1) * 10 + items.length) == total,
          user: req.session.user,
          success: req.flash('success').toString(),
          error: req.flash('error').toString()
        });
      });
    });

    app.get('/edititem/:itemid', function (req, res) {
      var currentUser = req.session.user;
      Item.edit(req.params.itemid, function (err, item) {
        if (err) {
          req.flash('error', err); 
          return res.redirect('back');
        }
        res.render('edititem', {
          title: '拉勾购 —— 关注拉勾购，享受好折扣 修改商品信息',
          item: item,
          //user: req.session.user,
          success: req.flash('success').toString(),
          error: req.flash('error').toString()
        });
      });
    });

    app.post('/edititem/:itemid', function (req, res) {
      var tag_baby = req.body.tag_baby,
            tag_toy = req.body.tag_toy,
            tag_cloth_man = req.body.tag_cloth_man,
            tag_cloth_lady = req.body.tag_cloth_lady,
            tag_cloth_shoe = req.body.tag_cloth_shoe;

        var tags = "";
        if(tag_baby) tags+='tag_baby;';
        if(tag_toy) tags+='tag_toy;';
        if(tag_cloth_man) tags+='tag_cloth_man;';
        if(tag_cloth_lady) tags+='tag_cloth_lady;';
        if(tag_cloth_shoe) tags+='tag_cloth_shoe;';

      var currentUser = req.session.user;
      Item.update(req.params.itemid, req.body.name, req.body.info, req.body.price, tags, req.body.imgpath, function (err) {
        var url = '/edititems';
        if (err) {
          req.flash('error', err); 
          return res.redirect(url);//出错！返回文章页
        }
        req.flash('success', '修改成功!');
        res.redirect(url);//成功！返回文章页
      });
    });

    app.get('/deleteitem/:id', function (req, res) {
      var currentUser = req.session.user;
      Item.remove(req.params.id, function (err) {
        if (err) {
          req.flash('error', err); 
          return res.redirect('back');
        }
        req.flash('success', '删除成功!');
        res.redirect('/edititems');
      });
    });

    app.get('/item/:id', function (req, res) {
      Item.getOne(req.params.id, function (err, item) {
        if (err) {
          req.flash('error', err); 
          return res.redirect('/');
        }
        res.render('item', {
          title: '拉勾购 —— 关注拉勾购，享受好折扣 '+item.name,
          item: item,
          user: req.session.user,
          success: req.flash('success').toString(),
          error: req.flash('error').toString()
        });
      });
    });

    app.get('/tag/:tag', function (req, res) {
      //判断是否是第一页，并把请求的页数转换成 number 类型
      var page = req.query.p ? parseInt(req.query.p) : 1;
      //查询并返回第 page 页的 10 个商品
      Item.getTenTag(req.params.tag, page, function (err, items, total) {
        if (err) {
          items = [];
        } 
        res.render('tag', {
          title: '拉勾购 —— 关注拉勾购，享受好折扣 '+req.params.tag,
          tag: req.params.tag,
          items: items,
          page: page,
          isFirstPage: (page - 1) == 0,
          isLastPage: ((page - 1) * 10 + items.length) == total,
          user: req.session.user,
          success: req.flash('success').toString(),
          error: req.flash('error').toString()
        });
      });
    });

    app.get('/popular', function (req, res) {
      //判断是否是第一页，并把请求的页数转换成 number 类型
      var page = req.query.p ? parseInt(req.query.p) : 1;
      //查询并返回第 page 页的 10 个商品
      Item.search("", "", page, function (err, items, total) {
        if (err) {
          items = [];
        } 
        res.render('search', {
          title: '拉勾购 —— 关注拉勾购，享受好折扣 '+req.params.keyword,
          search: req.params.keyword,
          sort: req.params.sort,
          items: items,
          page: page,
          isFirstPage: (page - 1) == 0,
          isLastPage: ((page - 1) * 10 + items.length) == total,
          user: req.session.user,
          success: req.flash('success').toString(),
          error: req.flash('error').toString()
        });
      });
    });
  
  app.use(function (req, res) {
    res.render("404");
  });

  function checkLogin(req, res, next) {
    if (!req.session.user) {
      req.flash('error', '未登录!'); 
      res.redirect('/login');
    }
    next();
  }

  function checkNotLogin(req, res, next) {
    if (req.session.user) {
      req.flash('error', '已登录!'); 
      res.redirect('back');
    }
    next();
  }
};